//
//  RecommentViewModel.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/5.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import Foundation
class  RecommendViewModel {

    lazy var  anchorCatgary:[AnchorCatgary]=[AnchorCatgary]()
    lazy var  lunbos:[LunBoBean]=[LunBoBean]()
    private lazy var face=AnchorCatgary()
    private lazy var bigData=AnchorCatgary()
}
extension RecommendViewModel{
    //  请求轮播数据
    func requsedLunBo(callBack:   @escaping ()->()){
        // http://capi.douyucdn.cn/api/v1/slide/6?version=2.300
        NetTools.requsetData(urlStr: getSlide6, type: .get, params: ["version":"2.300"]) { (result) in
            let resultDict =    result as! [String:Any]
            let dataArr =  resultDict["data"] as! [[String : Any]]
            for dict in dataArr{
                self.lunbos.append(LunBoBean(dict: dict))
            }
            callBack()
        }
        
    }
    
    
    //请求分类与房间数据
    func request(requstFinsh :  @escaping () -> ()){
        /**
         第一部分退时间
         http://capi.douyucdn.cn/api/v1/getbigDataRoom&time=1530770680
         **/
        //DispatchGroup分组管理异步任务 学习// https://blog.csdn.net/quan0981163/article/details/79601447
        let disGroup = DispatchGroup.init()
        disGroup.enter()
        NetTools.requsetData(urlStr: getbigDataRoom, type: .get, params: ["time":NSDate.getCurrentTimeSeconds()]) { (result) in
            let resultDict =   result as! [String:Any]
            let dataArr = resultDict["data"] as!  [[String:Any]]
            self.bigData.tag_name="热门"
            self.bigData.local_icon_url="home_header_hot"
            for dict in dataArr{
                self.bigData.roomListResult.append(RoomList(dict: dict))
            }
            
            disGroup.leave()
        }
        /**
         第二部分颜值
         http://capi.douyucdn.cn/api/v1/getVerticalRoom?limite=4&offset=0&time=1530770680
         **/
        disGroup.enter()
        NetTools.requsetData(urlStr: getVerticalRoom, type: .get) { (result) in
            let resultDict = result as! [String :Any]
            let dataArr = resultDict["data"] as! [[String:Any]]
            self.face.tag_name="颜值"
            self.face.local_icon_url="home_header_phone"
            
            for  (index , dict) in dataArr.enumerated(){
                if(index>3){//由于颜值 接口一次返回60个 所以手动控制 操出4个就不要了
                    break
                }
                self.face.roomListResult.append(RoomList(dict: dict))
            }
            disGroup.leave()
            
        }
        
        
        /***
         第三步份游戏
         http://capi.douyucdn.cn/api/v1/getHotCate?limite=4&offset=0&time=1530770680
         */
        disGroup.enter()
        NetTools.requsetData(urlStr: HOTCATE, type: .get, params: [ "limite" : "4" , "offset" : "0" , "time" : "\(NSDate.getCurrentTimeSeconds())" ]) { (result) in
            //将返回结果转成字典
            let resultDict =   result as! [String : Any]
            //获取data 这个key的值
            let  dataArr =  resultDict["data"] as! [[String:AnyObject]]
            for dict in dataArr{
                let item = AnchorCatgary(dict: dict)
                self.anchorCatgary.append(item)
            }
            disGroup.leave()
        }
        disGroup.notify(queue: .main) {
            self.anchorCatgary.insert(self.face, at: 0)
            self.anchorCatgary  .insert(self.bigData, at: 0)
            requstFinsh()
        }
    }
}
